//
//  LCEditFileTagVC.m
//  LongChain
//
//  Created by fengzi on 2018/6/18.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCEditFileTagVC.h"
#import "LCEditFileTagCell.h"

@interface LCEditFileTagVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *tagsCollection;

@property (weak, nonatomic) IBOutlet UITextField *tagTextField;

@property (nonatomic, strong) NSArray *tags;

@property (nonatomic, assign) int tagID;

@end

@implementation LCEditFileTagVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self loadTagsList];
}


#pragma mark - - UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger row = [indexPath row];

    LCEditFileTagCell *tagCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tagNameCell" forIndexPath:indexPath];
    if (self.tags.count > row) {
        NSDictionary *tagDic = self.tags[row];
        [tagCell updateFileTagWithName:tagDic];

        
    }
    return tagCell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.tags.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger row = [indexPath row];
    CGFloat height = 45;
    CGFloat width = 0;
    if (self.tags.count > row) {
        NSDictionary *tagDic = self.tags[row];
        NSString *tagName = [NSString stringWithFormat:@"%@",tagDic[@"name"]];
        
        CGRect rect = [tagName boundingRectWithSize:CGSizeMake(300, 100000) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} context:nil];
        
        width = (CGRectGetWidth(rect)) * 2 + 15;
        if (width < 40) {
            width = 40;
        }
        
    }
    return CGSizeMake(width,height);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCLog(@"选择标签");
}

#pragma mark - -

//  创建标签
- (IBAction)savaFileTag:(id)sender {
    
    NSString *tagName = self.tagTextField.text;
    if (tagName.length == 0) {
        [self showHint:@"标签名长度需要大于1"];
        return;
    }
    NSDictionary *tagDic = @{@"name":tagName};
    [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/nas/tag/create" parameter:tagDic responseSuccess:^(id resultCode, id resultParameter) {
        if ([resultCode isEqualToString:@"0"]) {
            
            NSString *tagID = [NSString stringWithFormat:@"%@",resultParameter[@"tag_id"]];
            self.tagID = [tagID intValue];
            [self updateFileTag];
        }else{
            
            [self showHint:resultParameter];
        }
        
    } responseFail:^(id resultCode, NSError *error) {
        
    }];
}

//  获取标签列表
- (void)loadTagsList {
    
    [[LCNetwork shareInstance]lc_getNetworkWithURL:@"/api/v0/nas/tag/list" responseSuccess:^(id resultCode, id resultParameter) {
        
        if ([resultCode isEqualToString:@"0"]) {
            LCLog(@"resultParameter = %@",resultParameter);
            NSArray *tagData = resultParameter[@"data"];
            self.tags = tagData;
            [self.tagsCollection reloadData];
            
        }
        
    } responseFail:^(id resultCode, NSError *error) {
        
    }];

}

//  更新文件标签
- (void)updateFileTag {
    
    NSDictionary *dic = @{@"tag_id":@[@(self.tagID)],@"file_id":@([self.fileID intValue])};
    [[LCNetwork shareInstance]lc_postNetworkWithRUL:@"/api/v0/nas/tag/file" parameter:dic responseSuccess:^(id resultCode, id resultParameter) {
        
        if ([resultCode isEqualToString:@"0"]) {
             //  修改单个文件成功
            [self.navigationController popViewControllerAnimated:YES];
        }
        LCLog(@"更新文件tag = %@",resultParameter);
        
    } responseFail:^(id resultCode, NSError *error) {
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
